package org.turbobytes.unikey.util;

/**
 * String Utilities
 *
 * @author Ganeshji Marwaha
 * @since Aug 7, 2010
 */
public class StringUtil {

  /**
   * Checks if 2 given strings are equal. It is null safe.
   * @param str1 - to check
   * @param str2 - to check
   * @return if the strings are equal
   */
  public static boolean equals(String str1, String str2) {
    if(str1 == null && str2 == null) return true;
    if(str1 == null || str2 == null) return false;
    return str1.equals(str2);
  }

  /**
   * Checks if the 2 given strings are equal ignoring the case. It is null safe
   * @param str1 - to check
   * @param str2 - to check
   * @return if the strings are equal or not ignoring case
   */
  public static boolean equalsIgnoreCase(String str1, String str2) {
    if(str1 == null && str2 == null) return true;
    if(str1 == null || str2 == null) return false;
    return str1.equalsIgnoreCase(str2);
  }

  /**
   * Checks if the given string is empty. Empty means null or zero length string
   * @param str to check
   * @return if the given string is empty or not
   */
  public static boolean isEmpty(String str) {
    if(str == null) return true;
    return str.length() == 0;
  }

  /**
   * Checks is the given string is not empty. Opposite of empty
   * @param str to check
   * @return if the given string is not empty or not
   */
  public static boolean isNotEmpty(String str) {
    return !isEmpty(str);
  }

  /**
   * Checks if the given string is blank. Blank means null, zero length string or string with just spaces
   * @param str to check
   * @return if the given string is blank or not
   */
  public static boolean isBlank(String str) {
    if(str == null) return true;
    return str.replaceAll("\\s+", "").length() == 0;
  }

  /**
   * Checks if the given string is not blank. Opposite of isBlank
   * @param str to check
   * @return if the given string is not blank or not
   */
  public static boolean isNotBlank(String str) {
    return !isBlank(str);  
  }

  /**
   * Trims the given string of spaces before and after it if any. Null safe.
   * @param str to check
   * @return trimmed string
   */
  public static String trim(String str) {
    if(str == null) return str;
    return str.trim();
  }
}
